package com.seasharp.lepoulpefiction;

import java.util.Iterator;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;

public class PoulpeFiction extends ApplicationAdapter {
		Texture dropImage;
		Texture bucketImage;
		Sound dropSound;
		Music rainMusic;
		SpriteBatch batch;
		OrthographicCamera camera;
		Rectangle bucket;
		Array<Goute> raindrops;
		long lastDropTime;
		Texture fond;
		// variable iteration
		int score;
		BitmapFont font;
		
	   @Override
	   public void create() {
		   font = new BitmapFont(Gdx.files.internal("data/CALIBRI-32.fnt"),Gdx . files . internal ( "data/fonts.png" ), false);
	      // load the images for the droplet and the bucket, 64x64 pixels each
		  bucketImage = new Texture(Gdx.files.internal("data/FormatFactorybucket.png"));

	      // load the drop sound effect and the rain background "music"
	      dropSound = Gdx.audio.newSound(Gdx.files.internal("data/FormatFactoryboom.wav"));
	      rainMusic = Gdx.audio.newMusic(Gdx.files.internal("data/FormatFactoryinternet.mp3"));

	      // start the playback of the background music immediately
	      rainMusic.setLooping(true);
	      rainMusic.play();

	      // create the camera and the SpriteBatch
	      camera = new OrthographicCamera();
	      camera.setToOrtho(false, 800, 480);
	      batch = new SpriteBatch();

	      // create a Rectangle to logically represent the bucket
	      bucket = new Rectangle();
	      bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally
	      bucket.y = 20; // bottom left corner of the bucket is 20 pixels above the bottom screen edge
	      bucket.width = 64;
	      bucket.height = 64;

	      // create the raindrops array and spawn the first raindrop
	      raindrops = new Array<Goute>();
          spawnRaindrop();
	   }

	
	      
	  private void spawnRaindrop() {
	        Goute raindrop = new Goute();
	        raindrop.rect.x = MathUtils.random(0, 800-64);
		    raindrop.rect.y = 480;
		    raindrop.rect.width = 64;
		    raindrop.rect.height = 64;
	        raindrops.add(raindrop);
	        lastDropTime = TimeUtils.nanoTime();
	   }

	   @Override
	   public void render() {
	      // clear the screen with a dark blue color. The
	      // arguments to glClearColor are the red, green
	      // blue and alpha component in the range [0,1]
	      // of the color to be used to clear the screen.
	      //Gdx.gl.glClearColor(0, 0, 0.2f, 1);
		  fond = new Texture(Gdx.files.internal("data/fond2.png"));
	      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
	    
	      // tell the camera to update its matrices.
	      camera.update();

	      // tell the SpriteBatch to render in the
	      // coordinate system specified by the camera.
	      batch.setProjectionMatrix(camera.combined);

	      // begin a new batch and draw the bucket and
	      // all drops
	      font.setColor(1f,1f,1f,1f);
	      batch.begin();
	      batch.draw(fond, 0, 0);
	      batch.draw(bucketImage, bucket.x, bucket.y);
	      font.draw(batch, "Score: " + score, 20, 440);
	      for(Goute raindrop: raindrops) {
	         batch.draw(raindrop.image, raindrop.rect.x, raindrop.rect.y);
	      }
	      batch.end();

	      // process user input
	      if(Gdx.input.isTouched()) {
	         Vector3 touchPos = new Vector3();
	         touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
	         camera.unproject(touchPos);
	         bucket.x = touchPos.x - 64 / 2;
	      }
	      if(Gdx.input.isKeyPressed(Keys.LEFT)) bucket.x -= 1000 * Gdx.graphics.getDeltaTime();
	      if(Gdx.input.isKeyPressed(Keys.RIGHT)) bucket.x += 1000 * Gdx.graphics.getDeltaTime();

	      // make sure the bucket stays within the screen bounds
	      if(bucket.x < 0) bucket.x = 0;
	      if(bucket.x > 800 - 64) bucket.x = 800 - 64;

	      // check if we need to create a new raindrop
	      if(TimeUtils.nanoTime() - lastDropTime > 500000000) spawnRaindrop();

	      // move the raindrops, remove any that are beneath the bottom edge of
	      // the screen or that hit the bucket. In the later case we play back
	      // a sound effect as well.
	      Iterator<Goute> iter = raindrops.iterator();
	      while(iter.hasNext()) {
	         Goute raindrop = iter.next();
	         raindrop.rect.y -= 200 * Gdx.graphics.getDeltaTime();
	         if(raindrop.rect.y + 64 < 0){
	        	 iter.remove();
	        	 score=score-2;
	         }
	         if(raindrop.rect.overlaps(bucket)) {
	        	 score++;
	            dropSound.play();
	            iter.remove();
	         }
	      }
	   }

	   @Override
	   public void dispose() {
	      // dispose of all the native resources
	      dropImage.dispose();
	      bucketImage.dispose();
	      dropSound.dispose();
	      rainMusic.dispose();
	      batch.dispose();
	   }

	   @Override
	   public void resize(int width, int height) {
	   }

	   @Override
	   public void pause() {
	   }

	   @Override
	   public void resume() {
	   }
	   class Goute{
		    Texture image;
		    Rectangle rect;
		    Goute(){
		        rect = new Rectangle();
		        rect.x = MathUtils.random(0, 800-64);
		        rect.y = 480;
		        rect.width = 64;
		        rect.height = 64;
		        switch(MathUtils.random(0, 5)){
		        case 0 :
					  image = new Texture(Gdx.files.internal("data/FormatFactorygoutea.png"));
				  break;
				  case 1 :
					  image = new Texture(Gdx.files.internal("data/FormatFactorygoute2.png")); 
				  break;
				  case 2 :
					  image = new Texture(Gdx.files.internal("data/FormatFactorygoute3.png")); 
				  break;
				  case 3 :
					  image = new Texture(Gdx.files.internal("data/FormatFactorygoute4.png")); 
				  break;
				  case 4 :
					  image = new Texture(Gdx.files.internal("data/FormatFactorygoute5.png")); 
				  break;
				  case 5 :
					  image = new Texture(Gdx.files.internal("data/FormatFactorygoute6.png")); 
				  break;
				  }
		        
		        }
		        
		    }
	}

